#include "Grammar.h"
#include "Combination.h"

namespace Pomian
{
    Grammar* Grammar::grammar()
    {
        Grammar* grammar = new Grammar();
        return grammar;
    }

    AstNode* Grammar::parse(Tokenizer* tokenizer)
    {
        if (m_parser != nullptr)
        {
            return m_parser->parse(tokenizer);
        }
        return nullptr;
    }

    Grammar* Grammar::option(IParser* parser)
    {
        m_parser = new Option(parser);
        return this;
    }

    Grammar* Grammar::repeat(IParser* parser)
    {
        m_parser = new Repeat(parser);
        return this;
    }

    Grammar* Grammar::o_r(const std::list<IParser*>& parsers)
    {
        m_parser = new Or(parsers);
        return this;
    }

    Grammar* Grammar::sequence(const std::list<IParser*>& parsers, const std::function<AstNode* (const std::vector<AstNode*>&)>& combine)
    {
        m_parser = new Sequence(parsers, combine);
        return this;
    }

    Grammar::~Grammar()
    {
        if (m_parser != nullptr)
        {
            delete m_parser;
        }
    }    
}